<template>
  <div style="width: 100%">
    <el-form
      ref="form"
      :disabled="disabled"
      :model="formDataAll"
      :rules="rules"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="服务名称" prop="jobGroup">
            <el-select v-model="formDataAll.jobGroup">
              <el-option
                v-for="item in serverList"
                :key="item.id"
                :label="item.appname"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="方法编码" prop="executorHandler">
            <el-input v-model="formDataAll.executorHandler"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="bean名称" prop="beanName">
            <el-input v-model="formDataAll.beanName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="方法名称" prop="methodName">
            <el-input v-model="formDataAll.methodName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="初始方法" prop="initMethod">
            <el-input v-model="formDataAll.initMethod"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销毁方法" prop="destroyMethod">
            <el-input v-model="formDataAll.destroyMethod"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="参数全路径" prop="paramTypes">
            <el-input v-model="formDataAll.paramTypes"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数名称" prop="paramName">
            <el-input v-model="formDataAll.paramName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="方法描述" prop="methodDesc">
            <el-input
              v-model="formDataAll.methodDesc"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="md-component-btns">
      <el-button size="medium" :disabled="disabled" type="primary" @click="save"
        >保存</el-button
      >
      <el-button size="medium" @click="$emit('removeThisTabs')">取消</el-button>
    </div>
  </div>
</template>
<script>
import * as api from "@/api/taskManage/taskManage";
export default {
  name: "globalMethodTemp",
  data() {
    return {
      dialogType: "add",
      currentData: {},
      formDataAll: {
        jobGroup: "",
        executorHandler: "",
        beanName: "",
        methodName: "",
        initMethod: "",
        destroyMethod: "",
        paramTypes: "",
        paramName: "",
        methodDesc: "",
      },
      rules: {
        jobGroup: [{ required: true, message: "请选择服务", trigger: "blur" }],
        executorHandler: [
          { required: true, message: "请输入方法编码", trigger: "blur" },
        ],
        beanName: [{ required: true, message: "beanName", trigger: "blur" }],
        methodName: [
          { required: true, message: "methodName", trigger: "blur" },
        ],
        methodDesc: [
          { required: true, message: "methodDesc", trigger: "blur" },
        ],
      },
      serverList: [],
    };
  },
  computed: {
    disabled() {
      return this.dialogType === "view";
    },
  },
  methods: {
    save() {
      this.$refs["form"].validate((f) => {
        if (f) {
          const paramData = {
            ...this.formDataAll,
          };
          if (this.dialogType === "add") delete paramData.id;
          let method = this.dialogType === "add" ? "addMethod" : "updateMethod";
          let msg = this.dialogType === "add" ? "新增" : "编辑";
          api[method](paramData).then(() => {
            this.$message.success(msg);
            this.$emit("refresh");
            this.$emit("removeThisTabs");
          });
        }
      });
    },
    getList() {
      api.getServiceList().then((res) => {
        this.serverList = res.data.data;
      });
    },
    initFormData(row) {
      const {
        jobGroup,
        executorHandler,
        beanName,
        methodName,
        initMethod,
        destroyMethod,
        paramTypes,
        paramName,
        methodDesc,
        id,
      } = row;

      this.formDataAll = {
        jobGroup,
        executorHandler,
        beanName,
        methodName,
        initMethod,
        destroyMethod,
        paramTypes,
        paramName,
        methodDesc,
        id,
      };
    },

    open() {
      this.dialogType = "add";
      this.getList();
    },
    edit(row) {
      this.dialogType = "edit";
      this.getList();
      this.currentData = { ...row };
      this.initFormData(row);
    },
    view(row) {
      this.dialogType = "view";
      this.getList();
      this.currentData = { ...row };
      this.initFormData(row);
    },
  },
};
</script>